package cn.thoughtworks.school.controllers;

import cn.thoughtworks.school.annotations.Auth;
import cn.thoughtworks.school.entities.Publisher;
import cn.thoughtworks.school.entities.User;
import cn.thoughtworks.school.services.PublishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "/api/publishers")
public class PublisherController {

  @Autowired
  PublishService publishService;

  @GetMapping
  public ResponseEntity getPublishersByCurrentOrganizationId(@Auth User user) {
    return ResponseEntity.ok(publishService.getPublishersByCurrentOrganizationId(user.getId()));
  }

  @GetMapping("/{userNameOrEmail}")
  public ResponseEntity findUserByNameOrEmail(@Auth User user, @PathVariable String userNameOrEmail){
    return ResponseEntity.ok(publishService.findUserByNameOrEmail(user, userNameOrEmail));
  }

  @PostMapping
  public ResponseEntity addPublisher(@Auth User user,@RequestBody Long  publisherId){
    Publisher publisher = publishService.addPublisher(user.getId(),publisherId);
    return new ResponseEntity(publisher, HttpStatus.CREATED);
  }

  @DeleteMapping("{id}")
  public ResponseEntity deletePublisher(@PathVariable Long id){
    publishService.deletePublisher(id);
    return ResponseEntity.noContent().build();
  }
}
